<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>C++模版 | Leon&#39;s blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言由于c++的模版学了很久都学不会,于是趁着这个周末整理一下有关函数模版的知识点">
<meta property="og:type" content="article">
<meta property="og:title" content="C++模版">
<meta property="og:url" content="https://leonlee233.github.io/2020/06/13/%E6%A8%A1%E7%89%88/index.html">
<meta property="og:site_name" content="Leon&#39;s blog">
<meta property="og:description" content="前言由于c++的模版学了很久都学不会,于是趁着这个周末整理一下有关函数模版的知识点">
<meta property="og:locale">
<meta property="article:published_time" content="2020-06-13T01:48:56.000Z">
<meta property="article:modified_time" content="2023-10-26T05:47:05.350Z">
<meta property="article:author" content="Leon Lee">
<meta property="article:tag" content="学习心得">
<meta property="article:tag" content="Hide">
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="@https:&#x2F;&#x2F;twitter.com&#x2F;leonwithleo">
  
    <link rel="alternate" href="/atom.xml" title="Leon's blog" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Leon&#39;s blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">主页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Suche"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://leonlee233.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-模版" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/06/13/%E6%A8%A1%E7%89%88/" class="article-date">
  <time class="dt-published" datetime="2020-06-13T01:48:56.000Z" itemprop="datePublished">2020-06-13</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      C++模版
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>由于c++的模版学了很久都学不会,于是趁着这个周末整理一下有关函数模版的知识点</p>
<span id="more"></span>  
<h3 id="1-函数模板"><a href="#1-函数模板" class="headerlink" title="1 函数模板"></a>1 函数模板</h3><h4 id="1-1定义函数模版的方法"><a href="#1-1定义函数模版的方法" class="headerlink" title="1.1定义函数模版的方法"></a>1.1定义函数模版的方法</h4><p>这里先给出一个实例,代码如下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">//定义函数模板</span><br><span class="line">template&lt;typename T&gt;//可以理解为一个声明,用T代替数据类型</span><br><span class="line">T add(T t1,T t2)//如若输入此函数的两个数据类型为int则</span><br><span class="line">&#123;               //函数将会实例化成为int add(int t1,int t2)</span><br><span class="line">	<span class="built_in">return</span> t1+t2;</span><br><span class="line">&#125; </span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>(1)</span><br><span class="line">	&#123;</span><br><span class="line">		cout&lt;&lt;<span class="string">&quot;请输入两个整数&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string">		int a1,a2;</span></span><br><span class="line"><span class="string">		cin&gt;&gt;a1&gt;&gt;a2; </span></span><br><span class="line"><span class="string">		cout&lt;&lt;&quot;这两个数之和为&quot;&lt;&lt;add(a1,a2)&lt;&lt;endl</span>;</span><br><span class="line">		cout&lt;&lt;<span class="string">&quot;请输入两个小数&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string">		double b1,b2;//此时函数实例化为:double add(double t1,double t2)</span></span><br><span class="line"><span class="string">		cin&gt;&gt;b1&gt;&gt;b2;</span></span><br><span class="line"><span class="string">		cout&lt;&lt;&quot;这两个数之和为&quot;&lt;&lt;add(b1,b2)&lt;&lt;endl</span>;</span><br><span class="line">		cout&lt;&lt;<span class="string">&quot;还要继续测试嘛,确认按1&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string">		int Temp;</span></span><br><span class="line"><span class="string">		cin&gt;&gt;Temp;</span></span><br><span class="line"><span class="string">		if(Temp != 1)</span></span><br><span class="line"><span class="string">		&#123;</span></span><br><span class="line"><span class="string">			break; </span></span><br><span class="line"><span class="string">		&#125; </span></span><br><span class="line"><span class="string">	&#125;</span></span><br><span class="line"><span class="string">	return 0; </span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>
<p><strong>测试图:</strong>  </p>
<p>具体方法解释如下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">template&lt;typename T&gt; T Max(T a,T b)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">return</span> a&gt;b ? a : b;</span><br><span class="line">&#125;</span><br><span class="line">/*template &lt;typename 模板形参表&gt; &lt;函数类型&gt; &lt;函数名&gt;(&lt;函数形参表&gt;) </span><br><span class="line">&#123;</span><br><span class="line"> 函数体</span><br><span class="line">&#125;</span><br><span class="line">template：定义模板的关键字，指明是函数或类模板。*/</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">	int m=10,n=20;</span><br><span class="line"> 	double x=12.56,y=3.5;</span><br><span class="line"> 	char a=<span class="string">&#x27;z&#x27;</span>,b=<span class="string">&#x27;w&#x27;</span>;</span><br><span class="line"> 	cout&lt;&lt;<span class="string">Max(m,n)&lt;&lt;endl;// 生成模板函数int Max</span>(int a, int b)</span><br><span class="line">	cout&lt;&lt;<span class="string">Max(x,y)&lt;&lt;endl; // 生成模板函数double Max</span>(double a, double b)</span><br><span class="line">	cout&lt;&lt;<span class="string">Max(a,b)&lt;&lt;endl; // 生成模板函数char Max</span>(char a, char b)</span><br><span class="line">	<span class="built_in">return</span> 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>测试图:</strong></p>
<p>函数模板的实例化有以下这几种方法:<br>1.隐式实例化<br>就如一开始的示例一样,通过传参的不同来判断函数模板的实例化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">int a1,a2;</span><br><span class="line">	cin&gt;&gt;a1&gt;&gt;a2; </span><br><span class="line">       //函数将会实例化成为int add(int t1,int t2)</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;这两个数之和为&quot;</span>&lt;&lt;<span class="string">add(a1,a2)&lt;&lt;endl;</span></span><br><span class="line"><span class="string">	double b1,b2;</span></span><br><span class="line"><span class="string">       //此时函数实例化为:double add</span>(double t1,double t2)</span><br><span class="line">	cin&gt;&gt;b1&gt;&gt;b2;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;这两个数之和为&quot;</span>&lt;&lt;<span class="string">add(b1,b2)&lt;&lt;endl;</span></span><br></pre></td></tr></table></figure>
<p>2.显式实例化<br>可以通过 template int add<int>(int t1,int t2)这样的语句进行显式实例化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">template &lt;typename T&gt;</span><br><span class="line">T add(T t1,T t2)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">return</span> t1+t2;</span><br><span class="line">&#125;</span><br><span class="line">template int add&lt;int&gt;(int t1,int t2);</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;10+&#x27;D&#x27;=&quot;</span>&lt;&lt;<span class="string">add&lt;int&gt;(10,&#x27;D&#x27;)&lt;&lt;endl;</span></span><br><span class="line"><span class="string">	cout&lt;&lt;&quot;1.3+2.4=&quot;&lt;&lt;add</span>(1.3,2.4)&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string">	system(&quot;pause&quot;);</span></span><br><span class="line"><span class="string">	return 0;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>
<p><strong>测试图:</strong></p>
<h4 id="1-2函数模板的重载"><a href="#1-2函数模板的重载" class="headerlink" title="1.2函数模板的重载"></a>1.2函数模板的重载</h4><p>函数模版重载方法如下<br>template<typename T><br>T 函数名(T 传入参数1,T 传入参数2)<br>{<br>    函数体;<br>}<br>template<typename T><br>T 相同的函数名(T 传入函数1,T 传入函数2,T 传入函数3)<br>{<br>    函数体;<br>}<br>具体使用方法如下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">template &lt;typename T&gt;</span><br><span class="line">T Max(const T&amp; t1,const T&amp; t2)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(t1 &gt; t2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">return</span> t1;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">return</span> t2;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">template &lt;typename T&gt;</span><br><span class="line">T Max(const T&amp; t1,const T&amp; t2,const T&amp; t3)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(t1 &gt;= t2)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(t1 &gt;= t3)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">return</span> t1; </span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">return</span> t3;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(t2 &gt;= t3)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">return</span> t2;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">return</span> t3;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">	double a,b,c;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;请输入三个数&quot;</span>&lt;&lt;<span class="string">endl; </span></span><br><span class="line"><span class="string">	cin&gt;&gt;a&gt;&gt;b&gt;&gt;c;</span></span><br><span class="line"><span class="string">	cout&lt;&lt;&quot;这仨个数中最大的数为&quot;&lt;&lt;Max(a,b,c)&lt;&lt;endl</span>;</span><br><span class="line">	int a1,b1; </span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;请输入两个数&quot;</span>&lt;&lt;<span class="string">endl; </span></span><br><span class="line"><span class="string">	cin&gt;&gt;a1&gt;&gt;b1;</span></span><br><span class="line"><span class="string">	cout&lt;&lt;&quot;这俩数中较大的数为&quot;&lt;&lt;Max(a1,b1)&lt;&lt;endl</span>; </span><br><span class="line">	<span class="built_in">return</span> 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-类模版"><a href="#2-类模版" class="headerlink" title="2.类模版"></a>2.类模版</h3><h4 id="2-1类模版的定义"><a href="#2-1类模版的定义" class="headerlink" title="2.1类模版的定义"></a>2.1类模版的定义</h4><p>定义:类模板可以用来描述一个与数据类型无关的类。类模板中类的<br>数据成员和成员函数的参数和返回值可以是任意的数据类型，<br>它描述了一组类的属性和行为，是一组类的统一描述，可以避<br>免类的重复定义。</p>
<p>定义类模板的一般格式为：<br>template &lt;模板形参表&gt;<br>class 类模板名<br>{<br> 类体<br>};</p>
<p>template&lt;typename 类型&gt;<br>class 类名<br>{<br>    类体(需要使用定义的类型)<br>}<br>template：定义模板的关键字，指明是函数或类模板。<br>模板形参表：用尖括号括起来。用来说明一个或多个类型形参和普通形参。多个模<br>板参数用“，”分隔。类型形参由关键字typename及其后面的标识符构成。该标<br>识符对应的实参可以是系统的基本数据类型，也可以是用户自定义的数据类型。<br>普通形参的说明方式为：&lt;类型&gt;&lt;普通形参名&gt;<br>下面是代码测试:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">template&lt;typename Type&gt;</span><br><span class="line">class Person</span><br><span class="line">&#123;</span><br><span class="line">	private:</span><br><span class="line">		Type m_money1,m_money2;</span><br><span class="line">	public:</span><br><span class="line">		<span class="function"><span class="title">Person</span></span>()&#123;&#125;</span><br><span class="line">		Person(Type a,Type b)</span><br><span class="line">		&#123;</span><br><span class="line">			m_money1=a;</span><br><span class="line">			m_money2=b;</span><br><span class="line">		&#125;</span><br><span class="line">		~<span class="function"><span class="title">Person</span></span>()&#123;&#125;</span><br><span class="line">		Type <span class="function"><span class="title">getmoney</span></span>()</span><br><span class="line">		&#123;</span><br><span class="line">			cout&lt;&lt;<span class="string">m_money1&lt;&lt;&quot; &quot;&lt;&lt;m_money2&lt;&lt;endl;</span></span><br><span class="line"><span class="string">			return m_money1</span>-m_money2;</span><br><span class="line">		&#125;</span><br><span class="line">&#125;;</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;请输入这个人的全部家当和负债&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string">	double m1,m2; </span></span><br><span class="line"><span class="string">	cin&gt;&gt;m1&gt;&gt;m2; </span></span><br><span class="line"><span class="string">	Person&lt;double&gt; P1(m1,m2);</span></span><br><span class="line"><span class="string">	cout&lt;&lt;&quot;他的纯资产为&quot;&lt;&lt;P1.getmoney()&lt;&lt;endl</span>;</span><br><span class="line">	<span class="built_in">return</span> 0; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-2-类模版的使用"><a href="#2-2-类模版的使用" class="headerlink" title="2.2 类模版的使用"></a>2.2 类模版的使用</h4><p>示例如下:<br>类模板的定义和使用。定义并使用Array类模板：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">// ArrayTemplate.h</span><br><span class="line"><span class="comment">#ifndef _ARRAY_H</span></span><br><span class="line"><span class="comment">#define _ARRAY_H</span></span><br><span class="line">template &lt;typename T, int length&gt;</span><br><span class="line">class Array</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">    T GetElement(int i)</span><br><span class="line">template &lt;typename T, int length&gt;</span><br><span class="line">void Array&lt;T, length&gt;::SetElement(T x)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">for</span> (int i=0;i&lt;GetLength();i++)</span><br><span class="line">    buffer[i]=x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">#endif</span></span><br><span class="line"> &#123; <span class="built_in">return</span> buffer[i]; &#125;</span><br><span class="line"> int <span class="function"><span class="title">GetLength</span></span>()</span><br><span class="line"> &#123; <span class="built_in">return</span> sizeof(buffer)/sizeof(T); &#125;</span><br><span class="line"> void SetElement(T x);</span><br><span class="line">private:</span><br><span class="line"> T buffer[length];</span><br><span class="line">&#125;;</span><br><span class="line">// testArrayTemplate.cpp</span><br><span class="line"><span class="comment">#include &quot;ArrayTemplate.h&quot;</span></span><br><span class="line"><span class="comment">#include &lt;iostream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line"> Array&lt;int,10&gt; a;</span><br><span class="line"> Array&lt;double,20&gt; b;</span><br><span class="line"> Array&lt;char,30&gt; c;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;对象a的数组长度为：&quot;</span>&lt;&lt; <span class="string">a.GetLength()&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;对象b的数组长度为：&quot;&lt;&lt; b.GetLength()&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;对象c的数组长度为：&quot;&lt;&lt; c.GetLength()&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> a</span>.SetElement(100);</span><br><span class="line"> b.SetElement(12.5);</span><br><span class="line"> c.SetElement(<span class="string">&#x27;w&#x27;</span>);</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;对象a的数组中第5个元素为：&quot;</span>&lt;&lt; <span class="string">a.GetElement(5)&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;对象b的数组中第10个元素为：&quot;&lt;&lt; b.GetElement(10)&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;对象c的数组中第20个元素为：&quot;&lt;&lt; c.GetElement(20)&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> return 0;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">  </span></span><br></pre></td></tr></table></figure>
<h4 id="2-3类模版静态成员"><a href="#2-3类模版静态成员" class="headerlink" title="2.3类模版静态成员"></a>2.3类模版静态成员</h4><p>类模板也允许有静态成员。静态成员将会是类模板实例化类的静态成员，<br>即对于一个类模板的每一个实例化类，它的所有对象共享类中的静态成员。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">template&lt;typename T&gt;</span><br><span class="line">class TA</span><br><span class="line">&#123;</span><br><span class="line"> …</span><br><span class="line"> static T m_t； //类模板的静态成员m_t</span><br><span class="line"> …</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>类模板的静态成员在定义类模板时不会被创建，在创建类模板的<br>实例化类后，在模板类中才会产生所有对象共享的静态成员。例如：</p>
<p>TA<int> iobj1, iobj2;<br>TA<double> dobj1, dobj2;</p>
<p>实例化模板类TA<int>的两个对象iobj1和iobj2将共享TA<int><br>的静态成员int m_t；</p>
<p>实例化模板类TA<double>的两个对象dobj1和dobj2将共享<br>TA<double>的静态成员double m_t。</p>
<p>由于静态数据成员不属于任何对象，所以在创建对象时也不会为该<br>类的静态数据成员分配存储空间。所以，类设计者需要在类外对静态<br>数据成员进行定义。静态数据成员的定义形式如下：<br> &lt;类型&gt; &lt;类名&gt;::&lt;静态数据成员名&gt;[&#x3D;&lt;初值&gt;];<br>例如：<br>int TA<int>::m_t&#x3D;100;<br>double TA<double>::m_t&#x3D;123.45;</p>
<h5 id="这一章似乎不是很重要的说"><a href="#这一章似乎不是很重要的说" class="headerlink" title="这一章似乎不是很重要的说"></a>这一章似乎不是很重要的说</h5>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://leonlee233.github.io/2020/06/13/%E6%A8%A1%E7%89%88/" data-id="clo6uzd8p000s8cukcw1gbw0x" data-title="C++模版" class="article-share-link"><span class="fa fa-share">Teilen</span></a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Hide/" rel="tag">Hide</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" rel="tag">学习心得</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/14/%E6%A8%A1%E7%89%88%E7%9A%84%E7%A4%BA%E4%BE%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          模版的示例
        
      </div>
    </a>
  
  
    <a href="/2020/06/07/%E5%A3%81%E7%BA%B8%E6%8E%A8%E8%8D%90/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">壁纸推荐</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hide/" rel="tag">Hide</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Literature-Review/" rel="tag">Literature Review</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9B%9E%E5%BF%86%E5%BD%95/" rel="tag">回忆录</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%A5%87%E6%80%AA%E7%9A%84%E7%A2%8E%E7%A2%8E%E5%BF%B5/" rel="tag">奇怪的碎碎念</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" rel="tag">学习心得</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag">学习笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%91%84%E5%BD%B1/" rel="tag">摄影</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%99%E7%A8%8B/" rel="tag">教程</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B8%B8%E8%AE%B0/" rel="tag">游记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BE%8E%E5%9B%BE/" rel="tag">美图</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Hide/" style="font-size: 20px;">Hide</a> <a href="/tags/Literature-Review/" style="font-size: 10px;">Literature Review</a> <a href="/tags/%E5%9B%9E%E5%BF%86%E5%BD%95/" style="font-size: 12px;">回忆录</a> <a href="/tags/%E5%A5%87%E6%80%AA%E7%9A%84%E7%A2%8E%E7%A2%8E%E5%BF%B5/" style="font-size: 10px;">奇怪的碎碎念</a> <a href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" style="font-size: 18px;">学习心得</a> <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" style="font-size: 10px;">学习笔记</a> <a href="/tags/%E6%91%84%E5%BD%B1/" style="font-size: 14px;">摄影</a> <a href="/tags/%E6%95%99%E7%A8%8B/" style="font-size: 16px;">教程</a> <a href="/tags/%E6%B8%B8%E8%AE%B0/" style="font-size: 12px;">游记</a> <a href="/tags/%E7%BE%8E%E5%9B%BE/" style="font-size: 10px;">美图</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/10/">October 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">November 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/10/26/DETR%E7%AC%94%E8%AE%B0/">文章总结：DETR笔记</a>
          </li>
        
          <li>
            <a href="/2023/07/14/%E4%B8%80%E4%B8%AA%E5%A5%BD%E7%9A%84%E5%BC%80%E5%A7%8B/">一个好的开始</a>
          </li>
        
          <li>
            <a href="/2021/11/20/linux%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%89%E8%A3%85/">linux系统的安装</a>
          </li>
        
          <li>
            <a href="/2021/11/20/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%9F%BA%E7%A1%80step1-hadoop/">大数据基础step1:hadoop</a>
          </li>
        
          <li>
            <a href="/2021/06/05/Linux%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">Linux学习笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 Leon Lee<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">主页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>